(function($){
var qryFlag='y';
var qryAttr='hasQry';
var defClass='open';
var parentClass='open_sub';
var hasRole='_hasRole';
var listKey="_listTreeKey";
var hoverClass='treeHover';
var _dataKey="_list_tree_data";
var _tree_pre="tree";

window.treeHandle={
	getTreeData:function(elem){
		return elem.data(_dataKey);
	},
	save:function(elem,val){
		elem.data(_dataKey,val);
	},
	exec:function(elem,name,param){
		var inst=elem.data('listTree');
		if(inst){
				return inst[name](param);
		}	
		return null;
	}	
};
$.fn.listTree=function(opt,param){
	this.each(function(){
		var $this=$(this);
		var inst=$this.data("listTree");
		if(!inst){
			inst=new $.listTree($this,$.extend({},$.listTree.defaults,opt));
			$this.data("listTree",inst);
		}
		else{
			inst.opt=$.extend({},$.listTree.defaults,opt);
			inst.init();
		}
	});
}
$.listTree=function(target,opt){
	this.dataMap={};
	this.keyAssist=0;
	this.target=target;
	this.opt=opt;
	//初始化查询参数
	//this.conditions={};
	this.init();
	this.drag=false;
}
$.listTree.prototype={
	getCurRow:function(){
		return this.curRow;
	},
	//获取实例属性
	getAttr:function(nodeName){
		return this[nodeName];
	},
	init:function(){
		this.tree=$("<div class='listTree'>").appendTo(this.target);
		//自定义样式
		if(this.opt.myClass){
			this.tree.addClass(this.opt.myClass);
		}
		this.body=$("<div class='listBody'>").appendTo(this.tree);
		this.curTop=this.body;
		this.bindQry();
	},
	//用于自定义静态数据 绑定
	bindTree:function(data){
		if(!data)
			return;
		var len=data.length;
		var hasChild;
		for(var i=0;i<len;i++){
			hasChild=false;
			if(data[i][this.opt.schema.subKey]&&data[i][this.opt.schema.subKey].length){
				hasChild=true;
			}
			var temp=this.initRow(data[i],hasChild).appendTo(this.curTop);
			this.opt.fillItem.call(this,data[i]);
			if(hasChild){
				var old=this.curTop;
				this.curTop=temp;
				this.bindTree(data[i][this.opt.schema.subKey]);
				this.curTop=old;
			}
		}
	},
	bindToolQry:function(qryName){
		var inst=this;
		if(!qryName||qryName.trim()==""){
			this.qryList.hide();
			this.body.show();
		}
		else{
			this.qryList.show();
			this.body.hide();
			//查询用户
			$.postJSON(this.opt.qryUrl,JSON.stringify({q:qryName.trim()}), function(data) {
				//data=$.decodeUTF(data);
				inst.qryList.html("");
				inst.curTop=inst.qryList;
				inst.bindItem(data.rows);
			});
		}
	},
	bindQry:function(){
		var inst=this;
		$.postJSON(this.opt.dataUrl,this.opt.conditions, function(data) {
			//只区分 静态书(一次性查询所有返回数据)与动态树 (每次根据父节点查询子节点) 
			inst.bindData(data);
		})
	},
	bindCheck:function(span,id){
		var ctrl=$("<input type='"+this.opt.checkType+"' name='name_"+this.opt.checkType+"' />").appendTo(span);
		if(id!=null){
			ctrl.attr("id","check_"+id);
		}
		ctrl.attr("callFunc","click:_list_tree_change");
	},
	getCanDelRow:function(elem){
		var ret;
		if(elem.hasClass(".tree_canDel")){
			ret=elem;
		}
		else{
			ret=elem.parent(".tree_canDel");
		}
		return ret;
	},
	//是否父节点由参数控制 不走this.opt.hasSub()
	getRow:function(param){
		return this.initRow(param.data,param.hasSub);
	},
	//添加子节点
	addSub:function(param){
		this.curTop=param.top?param.top:this.body;
		this.bindRow([param.data],param.noFill);
		return this.curRow;
	},
	initRow:function(data,hasSub){
		var showText='';
		var rowKey="";
		//父节点 ul
		if(hasSub){	
			showText=this.opt.schema.parentName;
			rowKey=this.opt.schema.parentIdKey;
			this.curRow=$("<ul class='tree_canDel hasSub'>");
		}
		//子节点 li
		else{
			showText=this.opt.schema.subName;
			rowKey=this.opt.schema.subIdKey;
			this.curRow=$("<li class='tree_canDel'>");
		}
		var inst=this;
		if(this.opt.hasCtrl&&hasSub){
			var ctrl=$("<span class='"+_tree_pre+"_ctrl' callFunc='click:noBubble'/>").appendTo(this.curRow);
			ctrl.bind("click",function(e){
				yanhx.exec('stopEvent',e);
				var $this=$(this);
				var pe=$this.parent();
				var hasQry=pe.attr("hasQry");
				if(!hasQry||hasQry!="y"){
					inst.qryChild(pe);	
				}
				if($this.hasClass(defClass)){
					$this.removeClass(defClass);
					pe.removeClass(parentClass);
				}
				else{
					$this.addClass(defClass);
					pe.addClass(parentClass);
				}
			});
		}
		//子节点放个空的占位符
		else if(this.opt.hasCtrl&&!hasSub){
			$("<span class='empty'></span>").appendTo(this.curRow);
		}
		if(this.opt.hasImg){
			$("<span class='"+_tree_pre+"_img' />").appendTo(this.curRow);
		}
		//有checkbox 或者radio
		if(this.opt.checkType){
			var checkSpan=$("<span class='"+_tree_pre+"_checkSpan checkSpan' />").appendTo(this.curRow);
			this.bindCheck(checkSpan,rowKey);			
		}
		//添加text
		var text=$("<span class='treeText' />").appendTo(this.curRow);
		text.text(data[showText]).bind("mouseover",this.textHover);
		text.text(data[showText]).bind("mouseout",this.textOut);
		//text 添加拖动事件
		if(this.opt.subDrag&&this.opt.parentDrag){
			dragHandle.myDrag({
				src:text,
				onDrop:this.opt.onDrop,
				isFocus:this.opt.noFocusDrag
			});
		}	
		return this.curRow;
	},
	textHover:function(){
		$(this).addClass("treeHover");
	},
	textOut:function(){
		$(this).removeClass("treeHover");
	},
	qryChild:function(elem){
		this.curTop=elem;
		this.curTop.attr("hasQry",'y');
		if(this.opt.staticTree){
			//静态树不处理
		}else if(this.opt.bindAll){
			//静态树不处理
		}else if(this.opt.bindQry){
			this.opt.setCond(elem,this);
			this.opt.bindQry(this.getData(elem));
		}
	},
	setCurTop:function(top){
		this.curTop=top;
	},
	reset:function(){
		this.curTop=this.body;
	},
	fillByUser:function(param){
		this.bindRow([{hasSub:param.hasSub}],true);
		this.curRow.append(param.elem);
		var ctrl=this.curTop.find(".tree_ctrl");
		if(!ctrl.hasClass(defClass)){
			ctrl.trigger("click");
		}
		return this.curRow;
	},
	bindData:function(data){
		if(this.opt.staticTree){
			this.bindFullTree(data);
		}
		else{
			this.bindRow(data);
		}
	},
	bindFullTree:function(data){
		var schema=this.opt.schema;
		this.fullData=data;
		this.fullModel=true;
		this.bindItem(data[schema.subKey]);
		//this.bindChild(data[schema.parentKey]);
	},
	bindItem:function(data){
		var inst=this;
		$(data).each(function(){
				inst.curTop.append(inst.initRow(this,false));
				inst.saveData(this);
				inst.opt.fillItem.call(inst,this);				
		});
	},
	bindRow:function(obj,noFillItem){
		//待修改
		var len=obj.length;
		var temp;
		
		for(var i=0;i<len;i++){
			temp=obj[i];
			if(!this.curTop||!this.curTop.parent().length){
				this.curTop=this.body;
			}
			this.curTop.append(this.initRow(obj[i],this.opt.hasSub(temp)));
			if(i==0&&len>1){
				this.curRow.addClass("tree_first");
			}
			if((i==(len-1)&&len>1)||len==1){
				this.curRow.addClass("tree_last");
			}
			this.saveData(temp);
			if(!noFillItem)
				this.opt.fillItem.call(this,temp);
		}
	},
	bindQrySub:function(obj){
		var schema=this.opt.schema;
		this.bindItem(obj[schema.subKey]);
	},
	closeSub:function(elem){
		elem.removeClass(defClass);
	},
	openSub:function(elem){
		elem.addClass(defClass);
		var hasQry=elem.attr(qryAttr);
		if(hasQry==qryFlag){
			return;
		}
		if(hasSub(elem,this)){
			this.initCondition(elem);
			this.bindQry();
		}
	},
	saveData:function(data){
		this.curRow.data(_dataKey,data);
	},
	getData:function(elem){
		return elem.data(_dataKey);
	},
	getKey:function(elem){
		var key=elem.attr(listKey);
		if(!key){
			key=listKey+"_"+this.keyAssist++;	
			elem.attr(listKey,key);
		}
		return key;
	},
	getDept:function(elem){
		var pa=elem.parent();
		if(!this.deptArr){
			this.deptArr=[];
		}
		if(pa.hasClass("listBody")){
			var temp=[];
			var len=this.deptArr.length;
			for(var i=len-1;i>=0;i--){
				temp[temp.length]=this.deptArr[i];
			}
			this.deptArr=null;
			return temp; 
		}
		else if(pa.get(0).tagName.toUpperCase()=="UL"){
			this.deptArr.push(pa.attr("text"));
			return this.getDept(pa);
		}
		else if(pa.get(0).tagName.toUpperCase()=="LI"){			
			return this.getDept(pa);
		}
	}
}
$.listTree.defaults={
	//查询子节点
	bindQry:null,
	myClass:null,
	//展现+号
	hasCtrl:true,
	//展现图标
	hasImg:false,
	conditions:{},
	//数据声明  判断是否有子节点
	schema:{
		subKey:'subs',
		subName:'name',
		subIdKey:'id',
		parentIdKey:'id',
		parentName:'name'
	},
	//一次性返回所有数据模式
	staticTree:false,
	//是否全部生成html 用于allData模式 
	bindAll:false,
	//单选 或多选 默认没有 
	checkType:null,
	//是否可以拖动
	parentDrag:false,
	//拖动时是否设置为焦点   用于是否需要获取拖动结束时 所处对象
	noFocusDrag:false,
	subDrag:false,
	//停止拖动事件
	onDrop:function(){},
	fillItem:function(data){},
	//判断是否有子节点
	hasSub:yanhx.findFunc("tree_hasSub"),
	//触发子节点查询 时  处理参数
	setCond:yanhx.findFunc("list_tree_qry_cond")
}	
})(jQuery);